<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="TextField（文本输入框）12345678910111213TextField(  decoration: InputDecoration(    icon: Icon(Icons.subject),    labelText: &#39;Title&#39;, &#x2F;&#x2F; 文字提示    hintText: &#39;Enter the post title&#39;, &amp;#">
<meta property="og:type" content="article">
<meta property="og:title" content="Flutter-输入框">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E8%BE%93%E5%85%A5%E6%A1%86/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="TextField（文本输入框）12345678910111213TextField(  decoration: InputDecoration(    icon: Icon(Icons.subject),    labelText: &#39;Title&#39;, &#x2F;&#x2F; 文字提示    hintText: &#39;Enter the post title&#39;, &amp;#">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-26T08:47:11.000Z">
<meta property="article:modified_time" content="2020-08-26T08:47:23.356Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="Flutter">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E8%BE%93%E5%85%A5%E6%A1%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Flutter-输入框 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E8%BE%93%E5%85%A5%E6%A1%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Flutter-输入框
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 16:47:11 / Modified: 16:47:23" itemprop="dateCreated datePublished" datetime="2020-08-26T16:47:11+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="TextField（文本输入框）"><a href="#TextField（文本输入框）" class="headerlink" title="TextField（文本输入框）"></a>TextField（文本输入框）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">TextField(</span><br><span class="line">  decoration: InputDecoration(</span><br><span class="line">    icon: Icon(Icons.subject),</span><br><span class="line">    labelText: &#39;Title&#39;, &#x2F;&#x2F; 文字提示</span><br><span class="line">    hintText: &#39;Enter the post title&#39;, &#x2F;&#x2F; 文字提示，类似placeholder</span><br><span class="line">    &#x2F;&#x2F; border: InputBorder.none, &#x2F;&#x2F; 不设置四周边框</span><br><span class="line">    &#x2F;&#x2F; border: OutlineInputBorder() &#x2F;&#x2F; 设置四周边框</span><br><span class="line">    filled: true, &#x2F;&#x2F; 是否填充背景</span><br><span class="line">    fillColor: Colors.grey, &#x2F;&#x2F; 填充颜色</span><br><span class="line">    &#x2F;&#x2F; onChanged: (value)&#123;&#125;, &#x2F;&#x2F; 当输入框内容发生变化时调用的函数</span><br><span class="line">    &#x2F;&#x2F; onSubmitted: (value)&#123;&#125;, &#x2F;&#x2F; 当点击确定时调用的函数</span><br><span class="line">  ),</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="decoration（InputDecoration）属性介绍"><a href="#decoration（InputDecoration）属性介绍" class="headerlink" title="decoration（InputDecoration）属性介绍:"></a>decoration（InputDecoration）属性介绍:</h3><table>
<thead>
<tr>
<th>属性</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>border</td>
<td>增加一个边框，</td>
</tr>
<tr>
<td>hintText</td>
<td>未输入文字时，输入框中的提示文字，</td>
</tr>
<tr>
<td>prefixIcon</td>
<td>输入框内侧左面的控件，</td>
</tr>
<tr>
<td>labelText</td>
<td>一个提示文字。输入框获取焦点/输入框有内容 会移动到左上角，否则在输入框内，labelTex的位置.</td>
</tr>
<tr>
<td>suffixIcon</td>
<td>输入框内侧右面的图标.</td>
</tr>
<tr>
<td>icon</td>
<td>输入框左侧添加图标</td>
</tr>
<tr>
<td>filled</td>
<td>是否填充背景</td>
</tr>
<tr>
<td>fillColor</td>
<td>填充颜色</td>
</tr>
<tr>
<td>onChanged</td>
<td>当输入框内容发生变化时调用的函数</td>
</tr>
<tr>
<td>onSubmited</td>
<td>当点击确定时调用的函数</td>
</tr>
<tr>
<td>errorText</td>
<td>错误提示文字</td>
</tr>
</tbody></table>
<h3 id="controller详解"><a href="#controller详解" class="headerlink" title="controller详解"></a>controller详解</h3><p><code>controller</code>可以理解为该文本框的映射，通过以下方式创建</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">TextEditingController textEditingController &#x3D; TextEditingController();</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">final textEditingController &#x3D; TextEditingController();</span><br></pre></td></tr></table></figure>
<p>以上两种创建方法等效，接下来，我们将要让控制器与对应文本框绑定</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">TextField(</span><br><span class="line">  controller: textEditingController, &#x2F;&#x2F; 控制器绑定</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>至此控制器就创建完成。另外，控制器还有两个默认的方法</p>
<h4 id="dispose"><a href="#dispose" class="headerlink" title="dispose"></a>dispose</h4><p>调用<code>dispose</code>的方法意为在使用完<code>controller</code>后自动卸载，从而优化性能</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@override</span><br><span class="line">void dispose()&#123;</span><br><span class="line">  textEditingController.dispose(); &#x2F;&#x2F; 卸载该控制器，类似于使用完成后卸载，将优化性能</span><br><span class="line">  super.dispose();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="initState"><a href="#initState" class="headerlink" title="initState"></a>initState</h4><p><code>initState</code>设置初始状态</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@override</span><br><span class="line">void initState()&#123; &#x2F;&#x2F; 设定控制器初始状态</span><br><span class="line">  super.initState();</span><br><span class="line">  textEditingController.text &#x3D; &#39;hi&#39;; &#x2F;&#x2F; 设置默认值</span><br><span class="line">  textEditingController.addListener( &#x2F;&#x2F; 添加事件监听</span><br><span class="line">    ()&#123;</span><br><span class="line">      debugPrint(&#39;input: $&#123;textEditingController.text&#125;&#39;);</span><br><span class="line">    &#125;</span><br><span class="line">  );</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/Flutter/" rel="tag"># Flutter</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/Flutter-Theme%EF%BC%88%E4%B8%BB%E9%A2%98%EF%BC%89/" rel="prev" title="Flutter-Theme（主题）">
      <i class="fa fa-chevron-left"></i> Flutter-Theme（主题）
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/Flutter-Form%EF%BC%88%E8%A1%A8%E5%8D%95%EF%BC%89%E5%92%8CSnackBar%EF%BC%88%E6%8F%90%E7%A4%BA%EF%BC%89/" rel="next" title="Flutter-Form（表单）和SnackBar（提示）">
      Flutter-Form（表单）和SnackBar（提示） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#TextField%EF%BC%88%E6%96%87%E6%9C%AC%E8%BE%93%E5%85%A5%E6%A1%86%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">TextField（文本输入框）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#decoration%EF%BC%88InputDecoration%EF%BC%89%E5%B1%9E%E6%80%A7%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">decoration（InputDecoration）属性介绍:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#controller%E8%AF%A6%E8%A7%A3"><span class="nav-number">1.2.</span> <span class="nav-text">controller详解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#dispose"><span class="nav-number">1.2.1.</span> <span class="nav-text">dispose</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#initState"><span class="nav-number">1.2.2.</span> <span class="nav-text">initState</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
